package com.margo.project.append.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.margo.project.append.utils.LcamGenerateHelper;

@Service
public class LcamServiceFactory {
	
	@Autowired
	private LcamGenerateHelper lcamGenerateHelper;
	
	public void createService(String tableName,String packageName) {
		StringBuilder sb=new StringBuilder();
		String className=lcamGenerateHelper.initFiledName(tableName.substring(5));
		sb.append("package ").append(packageName).append(".").append(className.toLowerCase()).append(".appservice; \r");
		sb.append(" \r");

		sb.append("import java.util.List; \r");
		sb.append("import java.util.Map; \r");

		sb.append("import com.comtop.lcam.project.common.exception.PSBusinessException; \r");
		sb.append("import com.comtop.lcam.project.sw.mobileservice.model.UserInfoVO; \r");
		sb.append("import com.comtop.util.commonservice.QueryCondition; \r");

		sb.append(" \r");

		String voName="Sw"+className+"VO";
		sb.append("import ").append(packageName).append(".").append(className.toLowerCase()).append(".model.").append(voName).append("; \r");
		sb.append(" \r");
		
		String serviceName="ISw"+className+"Service";
		sb.append("public interface ").append(serviceName).append(" {").append(" \r");
		//私有属性
		
		

		//queryCount
		sb
		.append("\t").append("public int query").append(className).append("Count(Map<String, String> queryMap,")
		.append("List<QueryCondition> condition);").append(" \r");
		
	    //queryList
		sb
		.append("\t").append("public List<").append(voName).append("> query").append(className).append("List(Map<String, String> queryMap, List<QueryCondition> condition,").append(" \r")
		.append("\t\t").append(" int pageNo, int pageSize);").append(" \r");

		
		//createOrUpdate
		sb.
		append("\t").append("public int createOrUpdateSw").append(className).append("(").append(voName).append(" ").append("sw"+className+"VO").append(",UserInfoVO creator) throws PSBusinessException;").append(" \r");
	    
		//read
		sb.append("\t").append(voName).append(" read").append(voName).append("(String ").append(className.substring(0, 1).toLowerCase())
		.append(className.substring(1)).append("Id);").append(" \r");
	    
		
		sb.append("}");
		lcamGenerateHelper.fillFile(className.toLowerCase()+"\\appservice",serviceName,sb);		
	}
	
	public void createServiceImpl(String tableName,String packageName) {
		StringBuilder sb=new StringBuilder();
		String className=lcamGenerateHelper.initFiledName(tableName.substring(5));
		sb.append("package ").append(packageName).append(".").append(className.toLowerCase()).append(".appservice.impl; \r");
		sb.append(" \r");

		sb.append("import java.util.List; \r");
		sb.append("import java.util.Map; \r");
		sb.append(" \r");
		
		sb.append("import javax.ejb.Remote; \r");
		sb.append("import javax.ejb.Stateless; \r");
		sb.append("import javax.ejb.TransactionAttribute; \r");
		sb.append("import javax.ejb.TransactionAttributeType; \r");
		sb.append("import javax.ejb.TransactionManagement; \r");
		sb.append("import javax.ejb.TransactionManagementType; \r");
		sb.append("import javax.interceptor.Interceptors; \r");
		
		sb.append("import com.comtop.lcam.common.appservice.EntityManager; \r");
		sb.append("import com.comtop.lcam.common.appservice.LcamBaseAppService; \r");
		sb.append("import com.comtop.lcam.common.ejb3.LcamDefaultInterceptor; \r");
		sb.append("import com.comtop.lcam.project.common.exception.PSBusinessException; \r");
		sb.append("import com.comtop.lcam.project.sw.mobileservice.model.UserInfoVO; \r");
		sb.append("import com.comtop.util.commonservice.QueryCondition; \r");

		sb.append(" \r");

		String voName="Sw"+className+"VO";
		String daoName="Sw"+className+"DAO";
		String serviceInterfaceName="ISw"+className+"Service";
		sb.append("import ").append(packageName).append(".").append(className.toLowerCase()).append(".model.").append(voName).append("; \r");
		sb.append("import ").append(packageName).append(".").append(className.toLowerCase()).append(".dao.").append(daoName).append("; \r");
		sb.append("import ").append(packageName).append(".").append(className.toLowerCase()).append(".appservice.").append(serviceInterfaceName).append("; \r");
		sb.append(" \r");
		
		String serviceName="ISw"+className+"Service";
		String serviceImplName="Sw"+className+"AppService";
		String ejbName="Sw"+className+"Ses";
		String ejbDescription="Sw"+className+"SesBean";
		
		sb.append("@Stateless(name = \"").append(ejbName).append("\", mappedName = \"ct\", description = \"")
			.append(ejbDescription).append("\") \r");
		sb.append("@Remote({ ").append(serviceName).append(".class }) \r");
		sb.append("@TransactionAttribute(TransactionAttributeType.REQUIRED) \r");
		sb.append("@TransactionManagement(TransactionManagementType.CONTAINER) \r");
		sb.append("@Interceptors({ LcamDefaultInterceptor.class }) \r");

		sb.append("public class ").append(serviceImplName)
			.append(" extends LcamBaseAppService implements ").append(serviceName)
			.append(" {").append(" \r");
		sb.append(" \r");
		//entityManager
		sb.append("\t").append("private final EntityManager entityManager = new EntityManager(); \r");
		sb.append(" \r");

		sb.append("\t").append("private final ").append(daoName).append(" ").append("sw"+className+"DAO").append("= new ").append(daoName).append("();\r");
		sb.append(" \r");
		
		String countMethodName="query"+className+"Count";
		//列表count方法
		sb.append("\t").append("/**  \r")
		  .append("\t").append(" * 查询列表数量  \r")
		  .append("\t").append(" */  \r");
		sb
		.append("\t").append("public int ").append(countMethodName).append("(Map<String, String> queryMap,")
		.append("List<QueryCondition> condition){").append(" \r");
		sb.append("\t\t").append("return this.").append("sw"+className+"DAO.").append(countMethodName).append("(queryMap,condition);").append(" \r");
		sb.append("\t").append("}").append(" \r");
		
		String listMethodName="query"+className+"List";
	    //列表list方法
		sb.append("\t").append("/**  \r")
		  .append("\t").append(" * 查询列表  \r")
		  .append("\t").append(" */  \r");
		sb
		.append("\t").append("public List<").append(voName).append("> ").append(listMethodName).append("(Map<String, String> queryMap, List<QueryCondition> condition,").append(" \r")
		.append("\t\t").append(" int pageNo, int pageSize){").append(" \r");		
		sb.append("\t\t").append("return this.").append("sw"+className+"DAO.").append(listMethodName).append("(queryMap,condition,pageNo,pageSize);").append(" \r");
		sb.append("\t").append("}").append(" \r");
		
		//create  或者保存
		sb.append("\t").append("/**  \r")
		  .append("\t").append(" * 保存更新vo  \r")
		 .append("\t").append(" */  \r");
		sb.append("\t").append("public int createOrUpdateSw").append(className).append("(").append(voName).append(" ").append("sw"+className+"VO").append(",UserInfoVO creator) throws PSBusinessException{").append(" \r");
		sb.append("\t\t").append("return 1;").append(" \r");
		sb.append("\t").append("}").append(" \r");
		//读取
		sb.append("\t").append("/**  \r")
		  .append("\t").append(" * 读取vo  \r")
		  .append("\t").append(" */  \r");
		sb.append("\t").append("public ").append(voName).append(" read").append(voName).append("(String ").append(className.substring(0, 1).toLowerCase())
		.append(className.substring(1)).append("Id){").append(" \r");
		sb.append("\t\t").append("return null;").append(" \r");
		sb.append("\t").append("}").append(" \r");
		sb.append("}");
		lcamGenerateHelper.fillFile(className.toLowerCase()+"\\appservice\\impl\\",serviceImplName,sb);	
	}
}
